<?php

require_once realpath (dirname (__FILE__)) . '//..//..//pages.php';
require_once CLASS_DATABASE;
require_once INC_DEFINES;
require_once INC_FUNCTIONS;

/**
 *
 * @author Andrea Cantafio
 */
class Record {
    /* Name of this table */

    protected $table;

    /* Values of this table */
    protected $values;

    /* Condition used to delete or update */
    protected $where;

    /* If set true this class will try to create a condition to identify this */
    protected $generate_where = true;

    /* If set true this class will avoid duplicates */
    protected $avoid_duplicates = true;
    
    protected $required = array ();

    /**
     * Creates a new table
     * 
     * @param string $table name of this table
     * @param array $values array containing values. Every value is paired with its field name using the corresponding key.
     * @param string $where condition used to delete or update database
     */
    public function __construct ($table, $values, $where = null) {
        $this->table = $table;
        $this->values = $values;
        if (isset ($where) && !empty ($where)) {
            $this->where = $where;
        } else if ($this->generate_where) {
            $this->rebuildWhere ();
        }
    }

    /**
     * This method unsets all the variables of this object 
     */
    public function __destruct () {
        foreach ($this as $key => $value) {
            unset ($this->$key);
        }
    }

    /**
     * This method returns a string representation of this object
     * @return string 
     */
    public function __toString () {
        $str = "Table: $this->table\nValues:\n";
        foreach ($this->values as $key => $value) {
            $str .= "\t$key=$value\n";
        }
        return $str;
    }

    public function exist ($where = null) {
        if (isset ($where) && !empty ($where)) {
            $this->where = $where;
        } else if ($this->generate_where) {
            $this->rebuildWhere ();
        }
        $db = openConnection ();
        if ($row = $db->fetchAssoc ($db->select ($this->table, 'DISTINCT *', $this->where, null, null, 1))) {
            return true;
        }
        return false;
    }

    public function get ($field) {
        if (isset ($this->values[$field])) {
            return $this->values[$field];
        }
        return false;
    }

    public function set ($field, $value) {
        if (!empty ($field)) {
            $this->values[$field] = $value;
        }
    }

    /**
     * This method deletes the corresponding record/s into the database
     * 
     * @param string $where condition to identify the records that will bw deleted
     * @return type 
     */
    public function delete ($where = null) {
        $db = openConnection ();
        if (isset ($where) && !empty ($where)) {
            $this->where = $where;
        }
        if (isset ($this->where) && !empty ($this->where)) {
            return $db->delete ($this->table, $this->where);
        }
        return false;
    }

    public function getTable () {
        return $this->table;
    }

    /**
     * This method returns values of this object
     * @return array 
     */
    public function getValues () {
        return $this->values;
    }

    public function getWhere () {
        return $this->where;
    }

    /**
     * Inserts this record into the database
     */
    public function insert ($ignore = true) {
        if ($this->avoid_duplicates && !$ignore) {
            if ($this->exist ()) {
                return false;
            }
        }
        if ($this->validate ()) {
            $db = openConnection ();
            return $db->insert ($this->table, $this->values, $ignore);
        }
        return false;
    }

    public function rebuildWhere () {
        if (isset ($this->values['ID'])) {
            $this->where = $this->table . '.ID=' . $this->values['ID'];
        } else {
            $where = '';
            foreach ($this->values as $key => $value) {
                $where .= "$this->table.$key='$value' AND ";
            }
            if (!empty ($where)) {
                $this->where = substr ($where, 0, strlen ($where) - 4);
            }
        }
    }

    public function reload ($where = null) {
        $db = openConnection ();
        if (isset ($where) && is_string ($where) && !empty ($where)) {
            $this->where = $where;
        }
        if (isset ($this->where) && !empty ($this->where)) {
            if ($row = $db->fetchAssoc ($db->select ($this->table, 'DISTINCT *', $this->where, null, null, 1))) {
                foreach ($row as $key => $value) {
                    $this->values[$key] = $value;
                }
                $this->rebuildWhere ();
                return true;
            }
        }
        return false;
    }

    /**
     * Updates the corresponding table in the database
     * @return type 
     */
    public function update ($where = null) {
        if (isset ($where) && is_string ($where)) {
            $this->where = $where;
        }
        $db = openConnection ();
        if (isset ($this->where) && !empty ($this->where)) {
            return $db->update ($this->table, $this->values, $this->where);
        }
        return false;
    }

    public function validate () {
        foreach ($this->required as $value) {
            if (empty($this->values[$value])) {
                return false;
            }
        }
        return true;
    }

}

?>
